﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Super.Common.Security;
using Super.Common.Validate;
using Super.DomainService;
using Super.Entities;
using Super.Filter;
using Super.Infrastructure;

namespace Super.Web.Areas.Admin.Controllers
{
    [SiteManageAuthorize(Check = false)]
    public class LoginController : BaseController
    {
        //
        // GET: /Admin/Login/

        public ActionResult Index()
        {
            var action = Query.Get("a");
            if (action == "out")
            {
                CookieService.DeleteUserCookies();
            }
            return View();
        }

        [HttpPost]
        public ActionResult Index(int? z)
        {

            var adp = new DbContextAdapter();
            var _userService = new BaseService<User>(adp);
            var passWord = MD5.GetMD5(Query.Post("password").Trim());
            var userName = Query.Post("username").Trim();
            var loginUser = _userService.Repository.Find(
                a =>
                a.Name.ToUpper().Equals(userName.ToUpper()) && a.PassWord == passWord)
                 .SingleOrDefault();

            if (loginUser == null)
            {
                PageTip("用户名或者密码错误！");
                return View();
            }
            CookieService.SaveUserCookies(loginUser);

            return View();
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        public ActionResult Out()
        {
            CookieService.DeleteUserCookies();
            return RedirectToAction("Index");
        }

    }
}
